﻿using System.Windows;
using System.Windows.Input;


namespace MPE
{
    public partial class Settings : Window
    {
        readonly System.Windows.Controls.Grid grid;
        readonly Properties.Settings param = Properties.Settings.Default;
        readonly System.Windows.Forms.Integration.WindowsFormsHost fieldProvider;
        public Settings(ref System.Windows.Controls.Grid grid, ref System.Windows.Forms.Integration.WindowsFormsHost fieldProvider)
        {
            InitializeComponent();
            this.grid = grid;
            this.fieldProvider = fieldProvider;

            fieldProvider.SizeChanged += FieldProvider_SizeChanged;

            if (param.taskFoler.Length > 0) taskForldLB.ToolTip = taskForldLB.Content = param.taskFoler;
            else
            {
                taskForldLB.ToolTip = taskForldLB.Content = "Папка не указана!";
                taskForldLB.Foreground = System.Windows.Media.Brushes.Red;
            }

            mapSize.Content = (int)codeMapSize.Value + "px";

            if (!param.isCodeMap)
            {
                showMSG.Visibility = Visibility.Visible;
                mapSize.Visibility = codeMapSize.Visibility = Visibility.Hidden;
            }
            codeMapSize.ValueChanged += CodeMapSize_ValueChanged;
        }
        void FieldProvider_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (param.isCodeMap)
            {
                showMSG.Visibility = Visibility.Hidden;
                mapSize.Visibility = codeMapSize.Visibility = Visibility.Visible;
            }
            else
            {
                showMSG.Visibility = Visibility.Visible;
                mapSize.Visibility = codeMapSize.Visibility = Visibility.Hidden;
            }
        }
        void UpdatesCB_Click(object sender, RoutedEventArgs e) {/* param.checkUpdates = !param.checkUpdates; param.Save();*/ }
        void FontChange_Click(object sender, RoutedEventArgs e)
        {
            using (System.Windows.Forms.FontDialog fontDialog = new System.Windows.Forms.FontDialog())
            {
                if (fontDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                { param.fieldFont = fieldProvider.Child.Font = fontDialog.Font; param.Save(); }
            }
        }
        void CodeMapSize_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (param.isCodeMap)
            {
                codeMapSize.ToolTip = "Ширина: " + codeMapSize.Value;
                param.codeMapSize = (int)codeMapSize.Value;
                mapSize.Content = (int)codeMapSize.Value + "px";
                grid.ColumnDefinitions[2].Width = new GridLength(codeMapSize.Value);
                param.Save();
            }
        }
        void SetTaskFolder_Click(object sender, RoutedEventArgs e)
        {
            using (System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog())
            {
                if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    taskForldLB.ToolTip = taskForldLB.Content = fbd.SelectedPath;
                    taskForldLB.Foreground = System.Windows.Media.Brushes.Black;
                    param.taskFoler = fbd.SelectedPath; param.Save();
                }
            }
        }
        void MakeBackUpCB_Click(object sender, RoutedEventArgs e) { param.isMakeBackUp = makeBackUpCB.IsChecked == true; param.Save(); }
        void OpenLastFileCB_Click(object sender, RoutedEventArgs e) { param.isOpenLastFile = openLastFileCB.IsChecked == true; param.Save(); }
        void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) => DragMove();
        void SaveWindowSizeCB_Click(object sender, RoutedEventArgs e) { param.isSaveWindowSize = saveWindowSizeCB.IsChecked == true; param.Save(); }
        void ForumTopic_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start("https://djoser-blog.ru/forum/index.php?/topic/7174-redaktor-shablonov-minipostereditor/"); }
        void GitHubSource_Click(object sender, RoutedEventArgs e) { System.Diagnostics.Process.Start("https://github.com/Medoo48/miniPosterEditor"); }
    }
}